var express = require('express');
var router = express.Router();
var path = require('path')
var bcrypt = require('bcryptjs')
var https = require('https')

/* GET home page. */
router.get('/getValidCode', function(req, res, next) {
  var mobile = req.query['mobile']
  var code = '0000' + Math.ceil(10000 * Math.random())
  code = code.slice(code.length - 4)
  var url = `https://106.ihuyi.com/webservice/sms.php?method=Submit&account=C58899095&password=9873d8e4a1678e82dc16fd91218c542c&mobile=${mobile}&content=您的验证码是：${code}。请不要把验证码泄露给其他人。`
  console.log('url:',url)
  https.get(url,function(response){
    var fullcontent = ''
    response.on('data',function(content){
        fullcontent +=content
    })
    response.on('end',function(){
        console.log('full:',fullcontent)
        var reg = /<code>(\d)<\/code>/gi
        var sendResult = reg.exec(fullcontent)[1]
        if(sendResult == 2){
            res.json({code:200,'msg':'ok',data:code})
        }else{
            res.json({code:500,'msg':'验证码发送失败！'})
        }
    })
  })
});

module.exports = router;
